package com.hddf.project.auth.pojos;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.alibaba.fastjson.annotation.JSONField;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Entity
@Table(name="users")
public class User {
	public static final String mongodb_collection_name = "user";
	/* id int not null auto_increment comment 'id',
	  username char(32) default null comment '用户名' ,
	  password char(32) not null comment '密码',
	  permission varchar(255) comment '权限',
	  department varchar(64)  comment '部门',
	  description varchar(255) comment '描述',
	  user_no  char(48) not null comment '用户号',
	  isopen tinyint not null default 1 comment '是否是有效用户',
	  mobile_phone varchar(24) comment '电话',
	  last_login_date datetime not null comment '最后一次登陆时间',
	  register_date datetime not null comment '注册时间',*/
	private int id;
	private String user_no;
	private String username;
	private String password ;
	private Date register_date;
	private Date last_login_date;
	private String department;
	private String description;
    private String mobile_phone;
	private int isopen = 1 ;
	private String permission;
	
	/**
	 * 将一个pojo对象转换为一个mongodb对象
	 * @return
	 */
	public DBObject toDbObject(){
        BasicDBObject entity = new BasicDBObject();
        //entity.put("_id", _id);
        entity.put("user_id", id);
        entity.put("user_no", user_no);
        entity.put("username", username);
        entity.put("password",password );
        entity.put("permission",permission);
        entity.put("register_date",register_date);
        entity.put("last_login_date", last_login_date);
        entity.put("department",department );
        entity.put("description", description);
        entity.put("mobile_phone",mobile_phone);
        entity.put("isopen",isopen);
       
        entity.put("web_token", "");
        entity.put("webtoken_lifetime",0);
        entity.put("app_token", "");
        entity.put("apptoken_lifetime",0);
        entity.put("pubk", "");
        entity.put("prik_map",null);
        entity.put("aes_key", "");
        return entity;
	}
	
	public void toUserObject(DBObject obj){
		//this._id=(String)obj.get("_id");
		if(null!=obj.get("user_id")){
			this.id = (Integer)obj.get("user_id");
		}
		this.user_no = (String)obj.get("user_no");
		this.username=(String)obj.get("username");
		this.password=(String)obj.get("password");
		this.permission=(String)obj.get("permission");
		this.register_date = (Date)obj.get("register_date");
		this.last_login_date = (Date)obj.get("last_login_date");
		this.department = (String)obj.get("department");
		this.description = (String)obj.get("description");
		this.mobile_phone=(String)obj.get("mobile_phone");
		if(null!=obj.get("isopen")){
			this.isopen=(Integer)obj.get("isopen");
		}
		
	}
	
	public User(){
		
	}
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY )
	@Column(name="id" ,nullable=false,unique=true)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="user_no" ,nullable=false)
	public String getUser_no() {
		return user_no;
	}
	public void setUser_no(String user_no) {
		this.user_no = user_no;
	}
	
	@Column(name="password" ,nullable=false,unique=false)
	@JSONField(serialize=false)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
	@Column(name="isopen" )
	@JSONField(serialize=false)
	public int getIsopen() {
		return isopen;
	}
	public void setIsopen(int isopen) {
		this.isopen = isopen;
	}
	
	@Column(name="mobile_phone" )
	public String getMobile_phone() {
		return mobile_phone;
	}
	public void setMobile_phone(String mobile_phone) {
		this.mobile_phone = mobile_phone;
	}

	@Column(name="username" )
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	
	@Column(name="register_date" )
	@JSONField(serialize=false)
	public Date getRegister_date() {
		return register_date;
	}

	public void setRegister_date(Date register_date) {
		this.register_date = register_date;
	}

	
	@Column(name="last_login_date" )
	@JSONField(serialize=false)
	public Date getLast_login_date() {
		return last_login_date;
	}

	public void setLast_login_date(Date last_login_date) {
		this.last_login_date = last_login_date;
	}

	
	
	@Column(name="department" )
	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@Column(name="description" )
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name="permission" )
	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}
	
	
	
	
}
